﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RestRT;
using Remindme.Repository.Interfaces;
using Remindme.Model;

namespace Remindme.Repository
{
    public class ArticleRepository : IArticleRepository
    {
        private RestClient restClient;

        public ArticleRepository()
        {
            restClient = new RestClient("http://www.diffbot.com/api/");
        }

        async public Task<ArticleModel> ResolveArticleAsync(string url)
        {
            IRestRequest request = new RestRequest("article?token={diffbot_token}&url={diffbot_url}");
            request.AddUrlSegment("diffbot_token", Other.Secrets.DIFFBOT_KEY);
            request.AddUrlSegment("diffbot_url", url);

            IRestResponse response = await restClient.ExecuteAsync(request);
            if (response.ErrorException != null) // Ocurrió un error en la petición
            {
                var ex = new Exceptions.CustomException(response.ErrorMessage);
                throw ex;
            }

            var deserializer = new RestRT.Deserializers.JsonDeserializer();
            var article = deserializer.Deserialize(response, typeof(ArticleModel));
            return article as ArticleModel;
        }

        public Task<Model.ArticleModel> CreateAsync(Model.ArticleModel obj)
        {
            throw new NotImplementedException();
        }

        public Task UpdateAsync(Model.ArticleModel obj)
        {
            throw new NotImplementedException();
        }

        public Task DeleteAsync(string objKey)
        {
            throw new NotImplementedException();
        }

        public Task<Model.ArticleModel> FindAsync(string objKey)
        {
            throw new NotImplementedException();
        }

        public Task<List<Model.ArticleModel>> FindAllAsync()
        {
            throw new NotImplementedException();
        }
    }
}
